package com.fire.control.system.module.device.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.fire.control.system.dto.CountDto;
import com.fire.control.system.dto.DeviceDTO;
import com.fire.control.system.dto.DeviceListDTO;
import com.fire.control.system.dto.MapDeviceDTO;
import com.fire.control.system.dto.SelectDeviceTypeDTO;
import com.fire.control.system.dto.app.AppDeviceInfoVo;
import com.fire.control.system.dto.app.AppDeviceVo;
import com.fire.control.system.dto.app.AppFloorVo;
import com.fire.control.system.dto.app.AppMapDeviceVo;
import com.fire.control.system.dto.app.AppMixVo;
import com.fire.control.system.dto.app.AppWarningInfoVo;
import com.fire.control.system.entity.Area;
import com.fire.control.system.entity.Device;

public interface DeviceMapper{
	
	@Update("update t_device set online_status=#{status} where id=#{deviceId}")
	public int updateDeviceOnlineStatus(@Param("deviceId")String deviceId ,@Param("status")String status);
	
	/**
	* @Title: getDeviceDetectionList
	* @Description: TODO(获取设备检测列表)
	* @author wanbo
	* @date  2017年6月29日 下午7:39:30
	* @return
	*/
	public List<Device> getDeviceDetectionList();
	
	/**
	 * @author: xiechengguang 
	 * @Title: countByCompanyId   
	 * @Description: 总数  
	 */
	public int countByCompanyId(@Param("companyId") String companyId, @Param("companyIds") List<String> companyIds);
	
	/**
	 * @author: xiechengguang 
	 * @Title: countOnLine   
	 * @Description: 在线数  
	 */
	public int countOnLine(@Param("companyId") String companyId, @Param("companyIds") List<String> companyIds);
	
	/**
	 * @author: xiechengguang 
	 * @Title: findByUserId   
	 * @Description: 获取当前app用户所负责的设备  
	 */
	public List<AppDeviceVo> findByUserId(@Param("userId") String userId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: findByCompanyId   
	 * @Description: 获取当前用户所在企业或者下拉选择企业的设备  
	 */
	public List<AppDeviceVo> findByCompanyId(@Param("companyId") String companyId, @Param("companyIds") List<String> companyIds);
	
	/**
	 * @author: xiechengguang 
	 * @Title: searchDeviceByName   
	 * @Description: 根据设备名称查询  
	 */
	public List<MapDeviceDTO> searchDeviceByName(@Param("searchKey") String searchKey, @Param("companyId") String companyId, @Param("companyIds") List<String> companyIds);
	
	@Select("select * from t_device where id = #{id}")
	public AppDeviceVo findById(@Param("id") String id);
	
	@Select("select * from t_device where id = #{deviceId}")
	public Device getById(@Param("deviceId") String deviceId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getDeviceList   
	 * @Description: 通过企业id获取设备列表  
	 */
	public List<DeviceListDTO> getDeviceList(@Param("groupId") String groupId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getDetailById   
	 * @Description: 根据id获取详细信息  
	 */
	public DeviceDTO getDetailById(@Param("id") String id);
	
	/**
	* @Title: getBydevEUI
	* @Description: TODO(根据设备序号查找设备信息)
	* @author wanbo
	* @date  2017年6月16日 上午11:44:51
	* @param devEUI
	* @return
	 */
	@Select("select a.*,replace(b.merger_name,',','') merger_name from t_device a left join t_area b on a.area_id=b.id where a.serial_number = #{devEUI}")
	public DeviceDTO getBydevEUI(@Param("devEUI") String devEUI);
	
	@Select("select a.*,replace(b.merger_name,',','') merger_name from t_device a left join t_area b on a.area_id=b.id where a.id = #{deviceId}")
	public DeviceDTO getBydevId(@Param("deviceId") String deviceId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: findByDeviceId   
	 * @Description: 根据设备id获取设备--报警相关信息 （侧面图） 
	 */
	public AppDeviceInfoVo findByDeviceId(@Param("deviceId") String deviceId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getByDeviceId   
	 * @Description: 根据设备id获取设备--报警相关信息 （平面图）   
	 */
	public AppWarningInfoVo getByDeviceId(@Param("deviceId") String deviceId);
	
	@Select("select g.group_name from t_device d left join t_device_group g on d.device_group_id = g.id where d.id = #{deviceId}")
	public String getGroupNameByDeviceId(@Param("deviceId") String deviceId);
	
	@Select("select f.access_address from t_device d left join t_file f on d.plan_id = f.id where d.id = #{deviceId}")
	public String getFileByDeviceId(@Param("deviceId") String deviceId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getFloors   
	 * @Description: 获取当前楼栋的每层的设备数  
	 */
	@Select("select floor, count(floor) as count from t_device where status = true AND device_group_id = #{groupId} GROUP BY floor")
	public List<AppFloorVo> getFloors(String groupId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: countThisFloor   
	 * @Description: 当前楼层总设备数  
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	@Select("select count(*) from t_device d where d.status = true AND d.device_group_id = #{groupId} and d.floor = #{floor}")
	public int countThisFloor(@Param("groupId") String groupId, @Param("floor") Integer floor);
	
	/**
	 * @author: xiechengguang 
	 * @Title: normalThisFloor   
	 * @Description: 当前楼层正常设备数    
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	@Select("select count(*) from t_device d where d.status = true AND d.device_group_id = #{groupId} and d.floor = #{floor} and d.online_status = 'nomal'")
	public int normalThisFloor(@Param("groupId") String groupId, @Param("floor") Integer floor);
	
	/**
	 * @author: xiechengguang 
	 * @Title: normalThisFloor   
	 * @Description: 当前楼层报警设备数    
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	@Select("select count(*) from t_device d where d.status = true AND d.device_group_id = #{groupId} and d.floor = #{floor} and d.online_status = 'alarm'")
	public int alarmThisFloor(@Param("groupId") String groupId, @Param("floor") Integer floor);
	
	/**
	 * @author: xiechengguang 
	 * @Title: normalThisFloor   
	 * @Description: 当前楼层故障设备数    
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	@Select("select count(*) from t_device d where d.status = true AND d.device_group_id = #{groupId} and d.floor = #{floor} and d.online_status = 'fault'")
	public int faultThisFloor(@Param("groupId") String groupId, @Param("floor") Integer floor);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getAllDeviceType   
	 * @Description: 获取所有的设备类型  
	 */
	@Select("select id,name from t_device_type")
	public List<SelectDeviceTypeDTO> getAllDeviceType();
	
	
	@Select("select * from t_area a where a.id = #{id}")
	public Area getAreaById(String id);
	
	@Update("update t_device d set d.status = false where d.id = #{id}")
	public int updateDeviceStatus(String id);
	
	/**
	 * @author: xiechengguang 
	 * @Title: searchDevices   
	 * @Description: 查询设备列表  
	 */
	public List<DeviceListDTO> searchDevices(@Param("deviceName") String deviceName, @Param("groupId") String groupId, @Param("onlineStatus") String onlineStatus);
	
	/**
	 * @author: xiechengguang 
	 * @Title: updateDevice   
	 * @Description: 更新设备实体  
	 */
	public void updateDevice(Device device);
	
	/**
	 * @author: xiechengguang 
	 * @Title: setOnlineStatus   
	 * @Description: TODO  
	 */
	@Update("update t_device d,t_warning_info w set d.online_status=#{status} where w.target_device_id = d.id AND w.id=#{warningId}")
	public int setOnlineStatus(@Param("warningId")String warningId ,@Param("status")String status);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getDevicesByTreeId   
	 * @Description: 根据省、市、区、设备组id及其搜索条件获取其下的对应设备  
	 */
	public List<DeviceListDTO> getDevicesByTreeId(@Param("id") String id, @Param("companyIds") List<String> companyIds, @Param("deviceName") String deviceName, @Param("onlineStatus") String onlineStatus);
	
	@Select("select d.device_group_id AS groupId,d.floor_max from t_device d where d.id = #{id}")
	public AppMixVo getGroupIdAndFloormaxByDeviceId(String id);
	
	@Select("select d.device_group_id from t_device d where d.id = #{id}")
	public String getGroupIdByDeviceId(String id);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getAllFloorsById   
	 * @Description: web端统计整栋楼每层设备数  
	 */
	public List<CountDto> getAllFloorsById(@Param("id") String id, @Param("groupId") String groupId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: eachFloorDevices   
	 * @Description: web端获取每层包含设备数  
	 */
	public List<MapDeviceDTO> eachFloorDevices(@Param("floor") Integer floor, @Param("groupId") String groupId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: getAllFloorsById   
	 * @Description: app端统计整栋楼每层设备数  
	 */
	public List<AppFloorVo> getAppAllFloorsById(@Param("id") String id, @Param("groupId") String groupId);
	
	/**
	 * @author: xiechengguang 
	 * @Title: eachFloorDevices   
	 * @Description: app端获取每层包含设备数  
	 */
	public List<AppMapDeviceVo> eachAppFloorDevices(@Param("floor") Integer floor, @Param("groupId") String groupId);
	
	
}
